package net.sdd.common.result;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

@ApiModel("通用返回结果")
@Data
public class Result implements Serializable {

    @ApiModelProperty("响应状态码")
    private Integer code;
    @ApiModelProperty("响应信息提示")
    private String message;
    @ApiModelProperty("响应数据")
    private Map<String,Object> data = new HashMap<>();

    // 返回当前Result对象，有默认的code、message值
    public static Result ok(){
        Result result = new Result();
        result.setCode(20000);
        result.setMessage("操作成功");
        return result;
    }

    public static Result fail() {
        Result result = new Result();
        result.setCode(50000);
        result.setMessage("系统异常，请联系管理员！");
        return result;
    }

    // 提供方法用于往result对象中存放数据
    public Result data(String key,Object value){
        this.data.put(key,value);
        return this;
    }

    // 可以直接传入map对象，最终返回
    public Result data(Map<String,Object> map){
        this.setData(map);
        return this;
    }

    public Result message(String message) {
        this.setMessage(message);
        return this;
    }

    public Result code(Integer code) {
        this.setCode(code);
        return this;
    }




}
